package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompTxnStatisMapper;
import cc.rengu.igas.bomp.common.entity.TxnStatis;
import cc.rengu.igas.bomp.common.enums.TxnTypeEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/04/18
 */
public class BompTxnStatisMapperImpl implements BompTxnStatisMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    @Override
    public List<TxnStatis> selectBompTxnStaticByDateAndTransNum(String instId, String statisDate, String totalTransNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompTxnStaticByDateAndTransNum");
        TxnStatis txnStatis = new TxnStatis();
        txnStatis.setInstId(instId);
        txnStatis.setStatisDate(statisDate);
        txnStatis.setTotalTransNum(totalTransNum);
        txnStatis.setTxnType(TxnTypeEnum.PAY.getTxnType());
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, txnStatis, TxnStatis.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户统计信息失败,instId:<{}>,statisDate:<{}>,totalTransNum:<{}>", instId, statisDate, totalTransNum);
            return null;
        }
        return objectList.stream().map(item -> (TxnStatis) item).collect(Collectors.toList());
    }
}
